package com.chf.job.controller;

import com.chf.job.common.Const;
import com.chf.job.common.ServerResponse;
import com.chf.job.pojo.*;
import com.chf.job.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ResumeController {
    @Autowired
    private ResumeService resumeService;
    @Autowired
    private FileService fileService;
    @Autowired
    private HopeJobService hopeJobService;
    @Autowired
    private UserService userService;
    @Autowired
    private WorkShowService workShowService;
    @Autowired
    private WorkExperService workExperService;
    @Autowired
    private EducationService educationService;

    /**
     * 更改简历名称
     */
    @RequestMapping("resume/updateResumeName.html")
    @ResponseBody
    public ServerResponse updateResumeName(HttpServletRequest request, @RequestParam("newResumeName") String newResumeName) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Resume userBaseInfo = resumeService.getUserBaseInfo(user.getEmail());
        if (userBaseInfo == null) {
            resumeService.addResume(user.getEmail());
        }
        int result = resumeService.updateResumeName(user.getEmail(), newResumeName);
        if (result > 0) {
            return ServerResponse.createBySuccess("更新成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("更新失败");

    }

    /**
     * 上传头像
     */
    @RequestMapping(value = "resume/uploadImage.html", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse uploadImage(@RequestParam("headPic") MultipartFile headPic, HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        String s = request.getSession().getServletContext().getRealPath("upload");
        String path = Const.IMAGE_URL;
        String upload = fileService.upload(headPic, path);
        // 写入书库
        int result = resumeService.updateHeardImg(user.getEmail(), upload);
        if (result > 0) {
            return ServerResponse.createBySuccess("上传头像成功", upload );
        }
        return ServerResponse.createByErrorMessage("上传失败");
    }

    /**
     * 期望工作
     */
    @RequestMapping(value = "resume/hopeJob.html", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addHopeJob(HopeJob hopeJob) {
        int result = hopeJobService.updateHopeJob(hopeJob);
        if (result > 0) {
            return ServerResponse.createBySuccess("跟新成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 更新基本信息
     */
    @RequestMapping(value = "resume/updateBaseInfo.html", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateBaseInfo(Resume resume, HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        userService.updateUserName(user.getEmail(), resume.getUserName());
        int result = resumeService.updateBaseInfo(resume);
        if (result > 0) {
            return ServerResponse.createBySuccess("更新成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 更新自我描述
     */
    @RequestMapping(value = "resume/updateMyDescription.html", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateMyDescription(Resume resume) {
        int result = resumeService.updateMyDescription(resume);
        if (result > 0) {
            return ServerResponse.createBySuccess("更新成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 删除自我描述
     */
    @RequestMapping("resume/deleteMyDesc.html")
    @ResponseBody
    public ServerResponse deleteMyDesc(@RequestParam("resumeId") Integer resumeId) {
        int result = resumeService.updateMyDesc(resumeId);
        if (result > 0) {
            return ServerResponse.createBySuccess("删除成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    /**
     * 添加作品展示
     */
    @RequestMapping(value = "resume/addWorkShow.html", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addWorkShow(WorkShow workShow) {
        int result = workShowService.addWorkShow(workShow);
        if (result > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("添加失败");
    }

    /**
     * 根据id删除作品
     */
    @RequestMapping(value = "resume/deleteWorkShow.html")
    @ResponseBody
    public ServerResponse deleteWorkShow(@RequestParam("resumeId") Integer resumeId) {
        int result = workShowService.deleteByResumeId(resumeId);
        if (result > 0) {
            return ServerResponse.createBySuccess("删除成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    /**
     * 添加工作经历
     */
    @RequestMapping(value = "resume/addWorkExper.html", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateWorkExper(WorkExper workExper) {
        int result = workExperService.updateWorkExper(workExper);
        if (result > 0) {
            return ServerResponse.createBySuccess("成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("失败");
    }

    /**
     * 删除工作经历
     */
    @RequestMapping("resume/deleteWorkExper.html")
    @ResponseBody
    public ServerResponse deleteWorkExper(@RequestParam("workExperId") Integer workExperId) {
        int result = workExperService.deleteById(workExperId);
        if (result > 0) {
            return ServerResponse.createBySuccess("删除成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    /**
     * 添加和更新教育背景
     */
    @RequestMapping(value = "resume/addEducation.html", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addEducation(Education education) {
        int result  = educationService.updateEducation(education);
        if (result > 0) {
            return ServerResponse.createBySuccess("成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("失败");
    }

    /**
     * 根据id删除教育背景
     */
    @RequestMapping("resume/deleteEducation.html")
    @ResponseBody
    public ServerResponse deleteEducation(@RequestParam("educationId") Integer educationId){
        int result = educationService.deleteById(educationId);
        if (result > 0) {
            return ServerResponse.createBySuccess("删除成功", Const.RESUME_PAGE);
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }


}